<template>
  <div class="rightbox" v-cloak>
    <h1>新增子集科室
      <el-button style="margin-left:20px;" type="primary" size="small" @click="addtext">新增文本</el-button>
      <el-button type="primary" size="small" @click="addimg">新增图片</el-button>
      <el-button type="primary" size="small" @click="addfile">新增视频</el-button>
    </h1>
    <div class="add-box" style="width:650px;">
      <el-form ref="form" :model="form" label-width="110px">
        <el-form-item
          label="排序序号"
          prop="sort"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input style="width:60px;" v-model="form.sort"></el-input>
        </el-form-item>
        <el-form-item
          label="科室名称"
          prop="name"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item
          label="科室简介"
          prop="remarks"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input type="textarea" v-model="form.remarks"></el-input>
        </el-form-item>
        <el-form-item
          label="科室地址"
          prop="address"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item
          label="状态"
          prop="state"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-radio v-model="form.state" label="1">启用</el-radio>
          <el-radio v-model="form.state" label="-1">禁用</el-radio>
        </el-form-item>
        <div v-for="(item, index) in form.contentList" v-bind:key="index">
          <el-form-item
            :prop="'contentList.' + index + '.value'"
            label="文字内容"
            v-if="item.type == 1"
            :rules="[
              { required: true, message: '不能为空', trigger: 'blur' }
            ]"
          >
            <el-input type="textarea" v-bind:label="item.name" v-model="item.value" style="width:350px;float:left;"></el-input>
            <el-button style="float:left;margin-left:20px;" @click="removeDomain(index,item)">删除</el-button>
          </el-form-item>
          <el-form-item
            :prop="'contentList.' + index + '.value'"
            label="内容图片"
            v-if="item.type == 2"
            :rules="[
              { required: true, message: '图片不能为空', trigger: 'blur' }
            ]"
          >
            <el-input type="file" v-model="item.value" v-bind:label="item.name" class="fileinput"  style="width:350px;float:left;"></el-input>
            <el-button style="float:left;margin-left:20px;" @click="removeDomain(index,item)">删除</el-button>
          </el-form-item>
          <el-form-item
            label="内容视频"
            v-if="item.type == 3"
            :prop="'contentList.' + index + '.value'"
            :rules="[
              { required: true, message: '视频不能为空', trigger: 'blur' }
            ]"
          >
            <el-input type="file" v-model="item.value" v-bind:label="item.name" class="fileinput"  style="width:350px;float:left;"></el-input>
            <el-button style="float:left;margin-left:20px;" @click="removeDomain(index,item)">删除</el-button>
          </el-form-item>
        </div>
        <el-form-item>
          <el-button type="primary" @click="submit('form')">提交</el-button>
          <el-button @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import axios from 'axios'
// import qs from 'qs'
import $ from 'jquery'
export default {
  data () {
    return {
      form: {
        sort: '',
        name: '',
        remarks: '',
        state: '1',
        addresss: '',
        contentList: []
      },
      departmentindex: '',
      superior: ''
    }
  },
  methods: {
    getFile (item, $event) {
      var file = $event.currentTarget.files[0]
      var reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = function () {
        item.value = this.result
      }
    },
    addtext: function () {
      var timestamp = new Date().getTime()
      this.form.contentList.push({
        type: 1,
        value: '',
        name: 'text' + timestamp
      })
    },
    addimg: function () {
      var timestamp = new Date().getTime()
      this.form.contentList.push({
        type: 2,
        value: '',
        name: 'img' + timestamp
      })
    },
    addfile: function () {
      var timestamp = new Date().getTime()
      this.form.contentList.push({
        type: 3,
        value: '',
        name: 'file' + timestamp
      })
    },
    removeDomain (index, item) {
      var inde = this.form.contentList.indexOf(item)
      if (inde !== -1) {
        this.form.contentList.splice(inde, 1)
      }
    },
    getParams () {
      var that = this
      that.departmentindex = that.$route.params.departmentindex
      that.superior = that.$route.params.superior
    },
    submit: function (form) {
      var that = this
      var formFile = new FormData()
      that.$refs[form].validate((valid) => {
        if (valid) {
          $('.fileinput input').each(function () {
            formFile.append($(this).attr('aria-label'), $(this).prop('files')[0])
          })
          formFile.append('Superior', that.departmentindex)
          formFile.append('Sort', that.form.sort)
          formFile.append('Name', that.form.name)
          formFile.append('Address', that.form.address)
          formFile.append('Remarks', that.form.remarks)
          formFile.append('State', that.form.state)
          formFile.append('fileModel', JSON.stringify(that.form.contentList))
          $.ajax({
            type: 'post',
            url: '/gp/Department/AddPost',
            dataType: 'json',
            data: formFile,
            processData: false,
            contentType: false,
            success: function (data) {
              if (data.code === 0) {
                that.$alert('新增成功', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {
                    that.resetForm(form)
                  }
                })
              } else {
                that.$pub.failcode(that, data.code, data.data)
              }
            },
            error: function (data) {
              that.$pub.catchbox(that)
            }
          })
        }
      })
    },
    resetForm (form) {
      this.$refs[form].resetFields()
      this.form.contentList = []
    }
  },
  mounted () {
    this.getParams()
  }
}
</script>
